package com.dj.dormitory.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dj.dormitory.commonutils.Result;
import com.dj.dormitory.entity.Bed;
import com.dj.dormitory.entity.Dormitory;
import com.dj.dormitory.entity.DormitoryStudent;
import com.dj.dormitory.service.BedService;
import com.dj.dormitory.service.DormitoryService;
import com.dj.dormitory.service.DormitoryStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author DJ同学
 * @since 2021-01-24
 */
@RestController
@RequestMapping("/dormitoryStudent")
public class DormitoryStudentController {

    @Autowired
    private DormitoryStudentService dormitoryStudentService;


    //根据宿舍查出所有学生姓名
    @PostMapping("getAll")
    public Result getAll(@RequestBody DormitoryStudent dormitoryStudent){
        List<Map<String,Object>> stus  =  dormitoryStudentService.getList(dormitoryStudent.getDormitoryId());
        return  Result.ok().data("list",stus);
    }
    @PostMapping("selectDormitory")
    public Result selectDormitory(@RequestBody DormitoryStudent dormitoryStudent, HttpServletRequest request) throws Exception {
        Integer studentId = (Integer) request.getAttribute("studentId");
        Integer res = dormitoryStudentService.selectDormitory(dormitoryStudent,studentId);
        if(res==1){
            return Result.error().message("你已经选择过床位啦");
        }else if(res==2){
            return Result.error().message("选择过床位错误");
        }else{
            return Result.ok().message("选择床位成功");
        }
    }
}

